package com.aurora_manito.xiaomi.service.impl;


import com.aurora_manito.xiaomi.entity.GoodsType;
import com.aurora_manito.xiaomi.entity.User;


import com.aurora_manito.xiaomi.mapper.UserMapper;
import com.aurora_manito.xiaomi.service.UserService;
import com.aurora_manito.xiaomi.utils.EmailUtils;
import com.aurora_manito.xiaomi.utils.Md5Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Aurora_Manito
 * @since 2020-05-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    private QueryWrapper<GoodsType> goodTypeWapper=new QueryWrapper<>();
    private Map<String,Object> condition=new HashMap<>();
    public User findUserWithUsernameAndPassword(User user){
        //创建一个条件对象
        condition.put("username",user.getUsername());
        condition.put("password",user.getPassword());
        List<User> userList = userMapper.selectByMap(condition);
        //判断范湖结果
        return userList!=null&&userList.size()==1?userList.get(0):null;
    }
    public User login(User user){
        //创建一个条件对象
        //将user的密码进行加密后比对
        user.setPassword(Md5Utils.md5(user.getPassword()));
        System.out.println("前端传入的密码"+user.getPassword());
        condition.put("username",user.getUsername());
        condition.put("password",user.getPassword());
        List<User> userList=userMapper.selectByMap(condition);;
        //判断范湖结果
        return userList!=null&&userList.size()==1?userList.get(0):null;
    }
    public String register(User user){
        //验证用户账号存在
        condition.put("username",user.getUsername());
        //根据条件对象查询数据
        List<User> userList=userMapper.selectByMap(condition);
        System.out.println(user);
        if(userList!=null&&userList.size()>0)
        {
            return "注册失败，账号已经存在，请使用其他用户注册";
        }
        //密码加密
        user.setPassword(Md5Utils.md5(user.getPassword()));
        System.out.println("加密成功");
        //添加用户
        userMapper.insert(user);
        System.out.println("添加用户成功");
        //发送邮件
        EmailUtils.sendEmail(user);
        return "注册成功";
    }
    public String checkUserName(String username){
        condition.put("username",username);
        //根据条件对象查询数据
        List<User> userList = userMapper.selectByMap(condition);
        if(userList!=null&&userList.size()>0)
        {
            return "用户名已经存在";
        }
        return "用户名可用";
    }

    @Override
    public boolean activate(String email, String code) {
        condition.put("email",email);
        condition.put("code",code);
        List<User> users = userMapper.selectByMap(condition);
        if(users!=null&&users.size()==1)
        {
            User user=users.get(0);
            user.setFlag(1);
            int i = userMapper.updateById(user);
            return i > 0;
        }
        return false;
    }
}
